package com.jiayou.codec

import com.jiayou.message.ChannelMessage
import com.jiayou.message.type.DataType
import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.ReplayingDecoder


/**
@author 李 双
@data   2020-11-18  16:21
@info
 */

open class ChannelDecoder : ReplayingDecoder<Unit>() {
    override fun decode(ctx: ChannelHandlerContext, `in`: ByteBuf, out: MutableList<Any>) {
        val length = `in`.readInt()
        val dataType = `in`.readInt()
        val bytes = ByteArray(length)
        `in`.readBytes(bytes)
        out.add(ChannelMessage(length, DataType::class.java.enumConstants[dataType], bytes))
    }
}